<?php
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );

  // -- webMS Modification --
  // Check if osCommerce environment is already loaded
  if (defined( '_OSCOMMERCE_LOADED' )) {
    if (!DB_SHARED) {
      // Reconnect to osCommerce Database
      tep_db_connect() or die('Unable to connect to database server!');
    }
    // Load osCommerce Globals for Boxes
    global $category_links, $languages_id, $catID, $catID_array, $current_category_id;
    global $request_type, $lng, $cart, $currencies, $language, $navigation, $breadcrumb;
  } else {
    // Load osCommerce Environment for the First Time
    require('components/'.$option.'/includes/application_top.php');
  }
define('EMAIL_TEXT_SUBJECTW', 'Subscription Notification');
define('EMAIL_TEXT_SUBJECTO', 'Overdue Notice #1');
define('EMAIL_TEXT_SUBJECTOO', 'Account Termination');
define('EMAIL_TEXT_EX', 'Your Account is up for renewal.  The subscription has been added to your shopping cart for your next visit to our site.');
define('EMAIL_TEXT_EXX', 'Your Account has become delinquent.  Please take care of this matter before your account is terminated.');
define('EMAIL_TEXT_EXXX', 'You have exceeded your subscriptions terms and your account is being terminated.');

define('EMAIL_TEXT_ORDER_NUMBER', 'Order Number:');
define('EMAIL_TEXT_INVOICE_URL', 'Detailed Invoice:');
define('EMAIL_TEXT_DATE_ORDERED', 'Date Sent:');
define('EMAIL_TEXT_PRODUCTS', 'Subscription for:');
define('EMAIL_TEXT_SUBTOTAL', 'Sub-Total:');
define('EMAIL_TEXT_TAX', 'Tax:        ');
define('EMAIL_TEXT_SHIPPING', 'Shipping: ');
define('EMAIL_TEXT_TOTAL', 'Total:    ');
define('EMAIL_TEXT_DELIVERY_ADDRESS', 'Delivery Address');
define('EMAIL_TEXT_BILLING_ADDRESS', 'Billing Address');
define('EMAIL_TEXT_PAYMENT_METHOD', 'Payment Method');
define('EMAIL_SEPARATOR', '------------------------------------------');





$res = tep_db_query("select * from " . TABLE_SUBSCRIPTION_CONFIG . "");
$qry = tep_db_fetch_array($res);

$newdate = date ("m/d/Y", mktime (0,0,0,date("m"),date("d")+$qry[warning],date("Y")));


$tcheck = tep_db_query("select * from " . TABLE_SUBSCRIPTION_QUE . " where bill_date = '" . $newdate . "'");
$c = tep_db_num_rows($tcheck);

if($c !=='0'){
while($go = tep_db_fetch_array($tcheck)){
//echo"$qry[warning] $c     $newdate $go[client_id]"; 
$co = tep_db_query("select * from " . TABLE_CUSTOMERS . " where customers_id = '" . $go[client_id] . "'");
$customer = tep_db_fetch_array($co);
$product_info_query = tep_db_query("select s.price_period, s.oneday_price, s.days_as_period, s.pay_period_type, p.products_id, pd.products_name, pd.products_description, p.products_model, p.products_quantity, p.products_image, pd.products_url, p.products_price, p.products_tax_class_id, p.products_date_added, p.products_date_available, p.manufacturers_id from " . TABLE_PRODUCTS . " p, ". TABLE_SUBSCRIPTIONS ." s, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_status = '1' and p.products_id = '" . $go['product_id'] . "' and pd.products_id = p.products_id and pd.language_id = '1' and s.productid = '" . $go['product_id'] . "'");
  $product_info = tep_db_fetch_array($product_info_query);
$new_prod = "$go[product_id]";
$new_prod_ex = "-$go[que_id]";
$next = "$new_prod$new_prod_ex"; 
$date = date ("m/d/Y");
$offset = NewDate($go[product_id]);
$ndate = date ("m/d/Y", mktime (0,0,0,date("m"),date("d")+$offset,date("Y")));

tep_db_query("INSERT INTO " . TABLE_CUSTOMERS_BASKET . " values ('', '" . $go[client_id] . "', '" . $next . "', '1', '', '" . $date . "')");
tep_db_query("INSERT INTO " . TABLE_SUBSCRIPTION . " values ('', '$go[client_id]', '$go[product_id]', '$go[o_order_id]', '$ndate', 'que', '', '')"); 
$id = tep_db_insert_id();
tep_db_query("INSERT INTO " . TABLE_SUBSCRIPTION_QUE . " values ('$id', '$go[client_id]', '$go[product_id]', '$go[o_order_id]', '$ndate', '$go[status]', '$go[order_id]', '$go[pay_date]')"); 

tep_db_query("update " . TABLE_SUBSCRIPTION . " set client_id= '$go[client_id]', product_id= '$go[product_id]', o_order_id= '$go[o_order_id]', bill_date= '$go[bill_date]', status= '$go[status]', order_id= '$go[order_id]', pay_date= '$go[pay_date]' where que_id= '$go[que_id]'"); 
tep_db_query("delete from " . TABLE_SUBSCRIPTION_QUE . " where que_id= '$go[que_id]'");



 $email_order = STORE_NAME . "\n" .
                 EMAIL_SEPARATOR . "\n\n" .
                 EMAIL_TEXT_EX . "\n\n" .
                 EMAIL_SEPARATOR . "\n\n" .
                 EMAIL_TEXT_PRODUCTS . ' ' . $product_info[products_name] . "\n" . 
                 EMAIL_TEXT_ORDER_NUMBER . ' ' . $go[order_id] . "\n" .
                 EMAIL_TEXT_INVOICE_URL . ' ' . tep_href_link(FILENAME_ACCOUNT_HISTORY_INFO, 'order_id=' . $go[o_order_id], 'SSL', false) . "\n" .
                 EMAIL_TEXT_DATE_ORDERED . ' ' . strftime(DATE_FORMAT_LONG) . "\n\n";
                 EMAIL_TEXT_TOTAL . ' ' . $go[price_period]  . "\n" .
                  EMAIL_SEPARATOR . "\n";

  tep_mail($customer['customers_firstname'] . ' ' . $customer['customers_lastname'], $customer['customers_email_address'], EMAIL_TEXT_SUBJECTW, $email_order, STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS);
}
}else{}

$newdate1 = date ("m/d/Y", mktime (0,0,0,date("m"),date("d")-$qry[notice1_days],date("Y")));
$tcheck1 = tep_db_query("select * from " . TABLE_SUBSCRIPTION . " where bill_date = '" . $newdate1 . "'");
$t = tep_db_num_rows($tcheck1);
if($t !== '0'){
while($go1 = tep_db_fetch_array($tcheck1)){
$co1 = tep_db_query("select * from " . TABLE_CUSTOMERS . " where customers_id = '$go1[client_id]'");
$customer1 = tep_db_fetch_array($co1);

$product_info_query = tep_db_query("select s.price_period, s.oneday_price, s.days_as_period, s.pay_period_type, p.products_id, pd.products_name, pd.products_description, p.products_model, p.products_quantity, p.products_image, pd.products_url, p.products_price, p.products_tax_class_id, p.products_date_added, p.products_date_available, p.manufacturers_id from " . TABLE_PRODUCTS . " p, ". TABLE_SUBSCRIPTIONS ." s, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_status = '1' and p.products_id = '" . $go1['product_id'] . "' and pd.products_id = p.products_id and pd.language_id = '1' and s.productid = '" . $go1['product_id'] . "'");
    $product_info = tep_db_fetch_array($product_info_query);
  $email_order = STORE_NAME . "\n" .
                 EMAIL_SEPARATOR . "\n" .
                 EMAIL_TEXT_EXX . "\n" . 
                 EMAIL_TEXT_ORDER_NUMBER . ' ' . $go1[order_id] . "\n" .
                 EMAIL_TEXT_INVOICE_URL . ' ' . tep_href_link(FILENAME_ACCOUNT_HISTORY_INFO, 'order_id=' . $go[o_order_id], 'SSL', false) . "\n" .
                 EMAIL_TEXT_DATE_ORDERED . ' ' . strftime(DATE_FORMAT_LONG) . "\n\n";
  $email_order .= $qry[notice1_notice]  .
                  EMAIL_SEPARATOR . "\n";

  tep_mail($customer1['customers_firstname'] . ' ' . $customer1['customers_lastname'], $customer1['customers_email_address'], EMAIL_TEXT_SUBJECTO, $email_order, STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS);
}
}else{}
$newdate2 = date ("m/d/Y", mktime (0,0,0,date("m"),date("d")-$qry[notice2_days],date("Y")));
$tcheck2 = tep_db_query("select * from " . TABLE_SUBSCRIPTION . " where bill_date = '" . $newdate2 . "'");
$t2 = tep_db_num_rows($tcheck2);
if($t2 !== '0'){

while($go2 = tep_db_fetch_array($tcheck2)){
$co2 = tep_db_query("select * from " . TABLE_CUSTOMERS . " where customers_id = '$go2[client_id]'");
$customer2 = tep_db_fetch_array($co2);

$product_info_query = tep_db_query("select s.price_period, s.oneday_price, s.days_as_period, s.pay_period_type, p.products_id, pd.products_name, pd.products_description, p.products_model, p.products_quantity, p.products_image, pd.products_url, p.products_price, p.products_tax_class_id, p.products_date_added, p.products_date_available, p.manufacturers_id from " . TABLE_PRODUCTS . " p, ". TABLE_SUBSCRIPTIONS ." s, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_status = '1' and p.products_id = '" . $go2['product_id'] . "' and pd.products_id = p.products_id and pd.language_id = '1' and s.productid = '" . $go2['product_id'] . "'");
    $product_info = tep_db_fetch_array($product_info_query);
  $email_order = STORE_NAME . "\n" .
                 EMAIL_SEPARATOR . "\n" .
                 EMAIL_TEXT_EXXX . "\n" . 
                 EMAIL_TEXT_ORDER_NUMBER . ' ' . $go2[order_id] . "\n" .
                 EMAIL_TEXT_INVOICE_URL . ' ' . tep_href_link(FILENAME_ACCOUNT_HISTORY_INFO, 'order_id=' . $go2[o_order_id], 'SSL', false) . "\n" .
                 EMAIL_TEXT_DATE_ORDERED . ' ' . strftime(DATE_FORMAT_LONG) . "\n\n";
  $email_order .= $qry[notice2_notice]  .
                  EMAIL_SEPARATOR . "\n";
tep_db_query("delete from " . TABLE_SUBSCRIPTION_QUE . " where que_id = '$go[que_id]'");
  tep_mail($customer2['customers_firstname'] . ' ' . $customer2['customers_lastname'], $customer2['customers_email_address'], EMAIL_TEXT_SUBJECTOO, $email_order, STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS);
}
}



?>
